import Vue from "vue";
import Router from "vue-router";

/* Router Modules */
import statistics from "./statistics";
import extension from "./extension";
import settlement from "./settlement";

Vue.use(Router);

/**
 * Note: sub-menu only appear when route children.length >= 1
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
  }
 */


export const errFunc = function(err) {
  console.log(err);
  const r = confirm("当前版本有更新，请刷新页面");
  if (r == true) {
    window.location.reload();
  }
};

export const constantRoutes = [
  {
    path: "/login",
    meta: {},
    component: () =>
      import("@/views/login").catch(err => {
        errFunc(err);
      })
  },
  {
    path: "/",
    redirect: "/statistics/overview",
    meta: {
      module: "statistics" // 用于菜单判断当前选中项
    },
    component: () => import("@/components/bussiness/layout"),
    children: [
      ...statistics, // 推广统计
      ...extension, // 推广管理
      ...settlement // 结算中心
    ]
  },
  {
    path: "/404",
    component: () =>
      import("@/views/404").catch(err => {
        errFunc(err);
      })
  },
  {
    path: "/roleperm",
    component: () =>
      import("@/views/roleperm").catch(err => {
        errFunc(err);
      })
  },
  { path: "*", redirect: "/", hidden: true }
];

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
